﻿using System;
using System.Windows;
using System.Windows.Input;

namespace testWPF
{
    public class MediaElementModel : ICommand
    {
        public bool isPlaying { set; get; }

        public MediaElementModel(MediaElementViewModel mediaElementViewModel)
        {
            // TODO: Complete member initialization
            this.mediaElementViewModel = mediaElementViewModel;
            this.isPlaying = false;
        }

        public bool CanExecute(object parameter)
        {
            return mediaElementViewModel.canExecuteCommand;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            string action = (string)parameter;
            switch (action.ToLower())
            {
                case "play":
                    if (isPlaying)
                        mediaElementViewModel.mediaView.Pause();
                    else
                        mediaElementViewModel.mediaView.Play();
                    isPlaying = isPlaying ? false : true;
                    mediaElementViewModel.mediaView.Visibility = isPlaying ? Visibility.Visible : Visibility.Hidden;
                    break;
                case "stop":
                    mediaElementViewModel.mediaView.Stop();
                    break;
                case "pause":
                    mediaElementViewModel.mediaView.Pause();
                    break;
                case "vol":
                    mediaElementViewModel.mediaView.IsMuted = mediaElementViewModel.mediaView.IsMuted ? false : true;
                    break;
                default:
                    throw new NotSupportedException(String.Format("Unknown media action {0}", action));
            }
        }

        private MediaElementViewModel mediaElementViewModel;
    }
}
